/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.rp.web.rp_house;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.thinkgem.jeesite.modules.sys.entity.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.rp.entity.rp_house.RpHouse;
import com.thinkgem.jeesite.modules.rp.service.rp_house.RpHouseService;
import java.util.List;

//package com.thinkgem.jeesite.modules.sys.web;






/**
 * 房屋管理Controller
 * @author 小牙白白
 * @version 2018-03-29
 */
@Controller
@RequestMapping(value = "${adminPath}/rp/rp_house/rpHouse")
public class RpHouseController extends BaseController {

	@Autowired
	private RpHouseService rpHouseService;

	@ModelAttribute
	public RpHouse get(@RequestParam(required = false) String id) {
		RpHouse entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = rpHouseService.get(id);
		}
		if (entity == null) {
			entity = new RpHouse();
		}
		return entity;
	}

	@RequiresPermissions("rp:rp_house:rpHouse:view")
	@RequestMapping(value = {"list", ""})
	public String list(RpHouse rpHouse, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RpHouse> page = rpHouseService.findPage(new Page<RpHouse>(request, response), rpHouse);
		model.addAttribute("page", page);
		return "modules/rp/rp_house/rpHouseList";
	}

	@RequiresPermissions("rp:rp_house:rpHouse:view")
	@RequestMapping(value = "form")
	public String form(RpHouse rpHouse, Model model) {
		model.addAttribute("rpHouse", rpHouse);
		return "modules/rp/rp_house/rpHouseForm";
	}

	@RequiresPermissions("rp:rp_house:rpHouse:edit")
	@RequestMapping(value = "save")
	public String save(RpHouse rpHouse, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, rpHouse)) {
			return form(rpHouse, model);
		}
		rpHouseService.save(rpHouse);
		addMessage(redirectAttributes, "保存房屋信息成功");
		return "redirect:" + Global.getAdminPath() + "/rp/rp_house/rpHouse/?repage";
	}

	@RequiresPermissions("rp:rp_house:rpHouse:edit")
	@RequestMapping(value = "delete")
	public String delete(RpHouse rpHouse, RedirectAttributes redirectAttributes) {
		rpHouseService.delete(rpHouse);
		addMessage(redirectAttributes, "删除房屋信息成功");
		return "redirect:" + Global.getAdminPath() + "/rp/rp_house/rpHouse/?repage";
	}

	@RequiresPermissions("rp:rp_house:rpHouse:edit")
	@RequestMapping(value = "import", method = RequestMethod.POST)
	public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/sys/user/list?repage";
		}
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<RpHouse> list = ei.getDataList(RpHouse.class);

			for (RpHouse rpHouse : list){
				try{
					rpHouseService.save(rpHouse);
 //					if ("true".equals(checkLoginName("", user.getLoginName()))){
//						user.setPassword(SystemService.entryptPassword("123456"));
//						BeanValidators.validateWithException(validator, user);
//						systemService.saveUser(user);
//						successNum++;
//					}else{
//						failureMsg.append("<br/>登录名 "+user.getLoginName()+" 已存在; ");
//						failureNum++;
//					}
				}catch(ConstraintViolationException ex){
//					failureMsg.append("<br/>登录名 "+user.getLoginName()+" 导入失败：");
//					List<String> messageList = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
//					for (String message : messageList){
//						failureMsg.append(message+"; ");
//						failureNum++;
//					}
				}catch (Exception ex) {
//					failureMsg.append("<br/>登录名 "+user.getLoginName()+" 导入失败："+ex.getMessage());
				}
			}
//			if (failureNum>0){
//				failureMsg.insert(0, "，失败 "+failureNum+" 条用户，导入信息如下：");
//			}
//			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条用户"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入用户失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/rp/rp_house/rphouse/list?repage";
	}

}